'use strict';

angular.module('app')
    .controller('creativeController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '创意列表';
            $scope.param = { };
            $scope.loading = false;
            $scope.selectPlatform = false;
            $scope.search = function (pageNum) {
                if(pageNum==1){
                    $scope.param.pageNum = 1;
                }
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/creative/list',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.selected = [];
            var updateSelected = function (action, contact) {
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }

                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };

            $scope.updateSelection = function ($event, contact) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
                    $scope.selected.push(contact.id);
                    contact.checked = true;
                }
                if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
                    $scope.selected.splice($scope.selected.indexOf(contact.id), 1);
                    contact.checked = false;
                }
            };
            // 全选操作
            $scope.selectAll = function ($event) {
                var checkbox = $event.target;
                var action = (checkbox.checked ? 'add' : 'remove');
                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    var contact = $scope.pageInfo.records[i];
                    updateSelected(action, contact);
                }
            };

            $scope.isSelected = function (id) {
                return $scope.selected.indexOf(id) >= 0;
            };

            $scope.search();
            //重置按钮的操作
            $scope.clearSearch = function() {
                $scope.param.keyword= null;
                $scope.param={};//将输入的数据清空
                $scope.search();
            }

            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.search();
            };
            //创意审核
            $scope.batchApprove = function (state) {
                if($scope.selected.length<=0){
                    $scope.alertModal({"content":"请选择要审核的项"});
                    return;
                }
                //判断是否有待审核的并记录有多少条
                var count = 0;
                var nocount = 0;
                angular.forEach($scope.pageInfo.records,function (item) {
                    if(item.status=='2'&&item.checked){
                        count++;
                    }else if(item.checked&&item.status!='2') {
                        nocount++;
                    }
                })
                if(nocount>0){
                    $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                    return;
                }
                $scope.alertMessage({
                    text: "确定做此操作吗？", //内容
                    btnCancel: true, //取消按钮
                    btnConfirm: true //确认按钮
                }).then(function () {
                    if (state == 3) {
                        $.ajax({
                            url: $scope.app.project_name + '/creative/batchApprove',
                            data: {'ids': $scope.selected, 'state': state}
                        }).then(function (result) {
                            $scope.loading = false;
                            if (result.httpCode == 200) {
                                var msg = result.errorMsg;
                                if (msg != '' && msg != null) {
                                    $scope.alertModal_slow({"content": msg});
                                } else {
                                    $scope.alertModal({"content": "操作成功"});
                                }
                                $('#refuseDiv').modal('hide');
                                $scope.selected=[];
                                $scope.search();
                            }
                        });
                    } else {
                        $scope.param.title = "创意";
                        $("#title").text($scope.param.title);
                        $('#refuseDiv').modal('show', true);
                    }

                })
            }
            // 审核失败保存
            $scope.saveApproveReason = function () {
                var reason = $scope.param.approveReason;
                if (reason == '' || reason == null) {
                    $scope.alertModal({"content": "请输入拒绝原因"});
                    $('#refuseDiv').modal('hide');
                    return;
                }

                $.ajax({
                    url: $scope.app.project_name + '/creative/batchApprove',
                    data: {'ids': $scope.selected, 'state': 4, 'reason': reason}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                    } else {
                        $scope.alertModal({"content": "操作失败"});
                    }
                    $scope.selected=[];
                    $('#refuseDiv').modal('hide');
                    $scope.search();
                });

            }

            $scope.selectmodal = function (item) {
                console.log(item);
                $.ajax({
                    url : $scope.app.project_name + '/creative/getPlatformList',
                    data: {'zzyAdId': item.zzyDdId,'creativeType':item.creativeType}
                }).then(function(result) {
                    console.log("======="+result.data);
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            $scope.selectPlatform = true;
                            $scope.platforms = result.data;
                        })

                    } else {
                        $scope.msg = result.msg;
                    }
                });

            }
    } ]);